/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pckODBC;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;

/**
 *
 * @author Portable-Maison
 */
public class ODBC_Connect {
    private static Connection gaConnection;


    public ODBC_Connect() // throws ClassNotFoundException
    {
    try
        {
        // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String url = "jdbc:odbc:GestionHoraire";
        gaConnection = DriverManager.getConnection(url);
        }
    catch (SQLException ex)
        {
            Logger.getLogger(ODBC_Connect.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("SQL Exception: "+ ex.toString());
        }
        catch (ClassNotFoundException cE)
            {
            System.out.println("Class Not Found Exception: "+ cE.toString());
            }
    }

//    public Vector chargerParametres()
//    {
//    Vector laVector = new Vector();
//    String query = "SELECT * FROM Parametre;";
//    ResultSet results;
//    try {
//        Statement stmt = gaCon.createStatement();
//        results = stmt.execute(query);
//        results.first ();
//        laVector.add (stmt);
//        }
//    catch(Exception e)
//        {
//        System.out.println("exception du a la requete");
//        }
//    }

    public Vector lectureParametre()
    {
    Vector laVector = new Vector();
    Statement stmt = null;
    ResultSet rs = null;
    //SQL query command
    String SQL = "SELECT * FROM Parametre"; // + psTable;
    try
    {
    stmt = gaConnection.createStatement();
    rs = stmt.executeQuery(SQL);
    while (rs.next())
        {
        laVector.add (rs.getString("NomEtudiant"));
        laVector.add (rs.getString ("idProgramme(FK)"));
        laVector.add (rs.getString("SessionCourante"));

        System.out.print (laVector.elementAt (0).toString () +
                          " : " + laVector.elementAt (1).toString () +
                          " : " + laVector.elementAt (2).toString ());
//        System.out.println(rs.getString("NomEleve") +
//                           " : " + rs.getString("Programme") +
//                           " : " + rs.getString("SessionCourante"));
        System.out.println("");
        }
    }
    catch (SQLException ex)
    {
        Logger.getLogger(ODBC_Connect.class.getName()).log(Level.SEVERE, null, ex);
    }
    return laVector;
    }

     public static void ScannerDriver()
    {
    for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();)
    {
        Driver driver = (Driver)e.nextElement();
        int majorVersion = driver.getMajorVersion();
        int minorVersion = driver.getMinorVersion();
        System.out.println("Driver = "+driver.getClass()+
            " v"+majorVersion+"."+minorVersion);
    }
    }
     
    public JComboBox listerProgramme(JComboBox paCombo)
    {
    Statement stmt = null;
    ResultSet rs = null;
    String SQL = "SELECT * FROM Programme"; 
    try
    {
    stmt = gaConnection.createStatement();
    rs = stmt.executeQuery(SQL);
    while (rs.next())
        {
        String lsNumero = (rs.getString("NumeroProgramme"));
        String lsNom = (rs.getString("NomCours"));
        paCombo.addItem (lsNumero + " - " + lsNom);
        System.out.println("");
        }
    }
    catch (SQLException ex)
    {
        Logger.getLogger(ODBC_Connect.class.getName()).log(Level.SEVERE, null, ex);
    }
    return paCombo;
    }
}
